package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName UserContoller
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2020/2/13 16:04
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/user")
public class UserContoller {

    @Reference// 订阅 dubbo注解
    UserService userService;

    // 使用SpringSecurity，获取当前登录的用户信息
    @RequestMapping(value = "/findUsername")
    public Result findUsername(){
        try {
            // 从SpringSecurity中获取用户信息
            User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            // 返回用户名，返回用户姓名，用户的地址...
            // com.itheima.health.pojo.User u = userService.findUserByUsername(user.getUsername());
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }

    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        // 传递当前页，每页显示的记录数，查询条件
        // 响应PageResult，封装总记录数，结果集
        PageResult pageResult = userService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.health.pojo.User user, Integer[] roleIds){
        try {
            userService.add(user,roleIds);
            //新增成功
            return new Result(true,MessageConstant.ADD_USER_SUCCESS);
        }catch (Exception e){
            //新增失败
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }

    }

    //根据id查询,回显检查组数据
    @RequestMapping(value = "/findById")
    public Result findById(Integer id){
        com.itheima.health.pojo.User user=userService.findById(id);
        if (user!=null){
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS,user);
        }else {
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
    }

    // 使用用户ID，查询用户ID所对应的角色的集合
    @RequestMapping(value = "/findRoleIdsByUserId")
    public List<Integer> findRoleIdsByUserId(Integer id){
        List<Integer> list = userService.findRoleIdsByUserId(id);
        return list;
    }

    // 修改
    @RequestMapping(value = "/edit")
    public Result edit(@RequestBody com.itheima.health.pojo.User user, Integer[] roleIds){
        try {
            userService.edit(user,roleIds);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
    }

    // 删除
    @RequestMapping(value = "/delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch(RuntimeException ex){
            ex.printStackTrace();
            return new Result(false, ex.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }
}
